<?php
/** Interfase of error setting/getting
*/
class IError
{
    var $_errors = array();

    /** Empty constructor
    */
    function IError(){}

    /** Returns stored errors
     * @param bool $clear clear errors
     * @return array errors
     */
    function getErrors($clear=true)
    {
        $err = $this->_errors;
        if ($clear)
            $this->_errors = array();
        return $err;
    }

    /** Adds errors (array of strings)
    */
    function addErrors($err, $params = array())
    {
        if (is_string($err))
            $this->_errors[] = _fmt($err, $params);
        elseif (is_array($err))
            foreach ($err as $e)
                $this->_errors[] = $e;
        else
            IError::fatal(__FUNCTION__.': Unknown type of input');
    }

    /**
     * Adds an error to object error buffer
     * @param string $k symbolic name of string constant
     * @param mixed $param var(s) for substitution ( array(0=>'Foo', 1=>'Bar') or 'Foo')
     * @return boolean false
     */
    function addError($k, $param=array())
    {
        if ($param && !is_array($param))
            $param = array($param);
        $this->_errors[] = _fmt($k, $param);
        return false;
    }

    /** Checks if current object has errors
     * @return bool
     */
    function hasErrors()
    {
        return sizeof($this->_errors);
    }

    /**
     *  Generates fatal error.
     *  @param string $mes reason
     */
    static function fatal($mes)
    {
        //Terminates app execution, raises user error
        trigger_error(substr($mes, 0, 4024), E_USER_ERROR);
        exit;
    }
}